using System.Collections.Generic;
using BCWAccountVip;
using I2.Loc;
using UnityEngine;

public class butExitToMenu : MonoBehaviour
{
	[SerializeField]
	private MessageBox box;

	[SerializeField]
	private UILabel quitLabel;

	[SerializeField]
	private GameObject endResultWindow;

	[SerializeField]
	private GameObject pauseWindow;

	private void Awake()
	{
		if (settings.offlineMode || GameController.instance.curTypeGame == TypeGame.normal)
		{
			quitLabel.text = ScriptLocalization.Get("Game_Common/vip_acc_endBattle");
		}
		else
		{
			quitLabel.text = ScriptLocalization.Get("Game_Common/leave_ok_button");
		}
	}

	private void OnClick()
	{
		if (GameController.instance.curTypeGame == TypeGame.normal && GameplayEventsManager.instance != null && GameController.instance != null)
		{
			GameplayEventsManager.instance.SaveCounter(GameController.instance.playerScript.Points, settings.keySandboxKills);
		}
		EventDelegate.Callback callback;
		if (settings.offlineMode || GameController.instance.curTypeGame == TypeGame.normal)
		{
			if (IOSInternetConnection.isEnable)
			{
				GoToLobby();
				return;
			}
			callback = ShowRewards;
		}
		else
		{
			callback = GoToLobby;
		}
		if (ManagerMissions.currMission != null)
		{
			box.Show("Game_Pause/lb_mission_prigress_lost", callback);
		}
		else if (!settings.offlineMode && GameInfoWindow.instance.resultWindow != null)
		{
			int getawayResult = 0;
			if (GameInfoWindow.instance.resultWindow.ReturnGetawayResult(ref getawayResult) && getawayResult < 0)
			{
				box.Show("Game_Common/leave_discription", callback);
			}
			else
			{
				callback();
			}
		}
		else
		{
			callback();
		}
	}

	public void ShowRewards()
	{
		if (PlayerBehavior.MyPlayer != null)
		{
			PlayerBehavior.MyPlayer.DeactivateAllChargesOnline();
		}
		if (GameController.instance.playerScript != null)
		{
			if (GameController.instance.playerScript.inCar || GameController.instance.playerScript.inHelic)
			{
				GameController.instance.playerScript.isNeedGetOut = true;
				GameController.instance.playerScript.GetOutOfCar(false);
			}
			GameController.instance.playerScript.transform.position = Vector3.up * 1000f;
			GameController.instance.playerScript.SetActiveComponentBesidesControl(false);
			GameController.instance.playerScript.SetVisiblePlayerBesidesControl(false);
		}
		if (PrivateCarManager.instance != null && PrivateCarManager.instance.currentCar != null)
		{
			PrivateCarManager.instance.currentCar.transform.position = Vector3.up * -1000f;
		}
		PanelEndGameRewardUI componentInChildren = endResultWindow.GetComponentInChildren<PanelEndGameRewardUI>();
		componentInChildren.CalculateReward();
		if (componentInChildren.RewardListSimple.Count > 0 || componentInChildren.SessionKillCount != 0)
		{
			pauseWindow.SetActive(false);
			endResultWindow.SetActive(true);
			componentInChildren.Callback = GoToLobby;
			componentInChildren.IsActiveVip = ManagerVip.GetInstance().IsActiveVip();
			componentInChildren.Show();
		}
		else
		{
			GoToLobby();
		}
	}

	public void GoToLobby()
	{
		ADSLocation leaveLocation;
		if (settings.offlineMode)
		{
			leaveLocation = ADSLocation.LeaveAdventure;
		}
		else
		{
			if (FlurryWrapper.activePlayer && GameController.instance != null && GameController.instance.curTypeGame == TypeGame.normal)
			{
				BCWStorage.Set(FlurryWrapper.keyDayCash, BCWStorage.Get<int>(FlurryWrapper.keyDayCash) + settings.cathedExeptionsForFlurry);
				BCWStorage.Set(FlurryWrapper.keyDayGold, BCWStorage.Get<int>(FlurryWrapper.keyDayGold) + settings.cathedWarningstForFlurry);
				int num = 0;
				if (JSONManager.Instance.ServerTimeLoad())
				{
					num = JSONManager.Instance.GetServerTime() - GameController.instance._startTimer;
				}
				if (settings.cathedExeptionsForFlurry > 0 && num != 0)
				{
					Dictionary<string, string> dictionary = new Dictionary<string, string>();
					dictionary.Add(FlurryWrapper.TimeForSandbox(num), FlurryWrapper.MoneyForSandbox(settings.cathedExeptionsForFlurry));
					FlurryWrapper.LogEvent(FlurryWrapper.EV_DAILY_EARNED_CASH, dictionary);
				}
				if (settings.cathedWarningstForFlurry > 0 && num != 0)
				{
					Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
					dictionary2.Add(FlurryWrapper.TimeForSandbox(num), FlurryWrapper.GoldForSandbox(settings.cathedWarningstForFlurry));
					FlurryWrapper.LogEvent(FlurryWrapper.EV_DAILY_EARNED_GOLD, dictionary2);
				}
			}
			leaveLocation = ((GameController.instance.curTypeGame != TypeGame.deathmath) ? ADSLocation.LeaveSandbox : ADSLocation.LeavePvp);
		}
		settings.cathedExeptionsForFlurry = 0;
		settings.cathedWarningstForFlurry = 0;
		if (FyberWrapper.instance != null)
		{
			FyberWrapper.instance.leaveLocation = leaveLocation;
			FyberWrapper.instance.ShowAds(ADSLocation.TransilitionToThePost);
		}
		if (GameController.instance != null)
		{
			GameController.instance.ChangeLevel();
		}
		settings.playSoundButton();
		controllerConnectPhoton.thisScript.changeMap = false;
		GameController.instance.exitToMenu();
		Save.SaveBool(settings.keyLoadPVPMenu, true);
		settings.loadFromGame = true;
	}
}
